﻿using System;
using System.Linq;
using System.Windows.Forms;
using AppAcc.Models;

namespace AppAcc.Views.DanhMuc
{
    public partial class FormHangHoa : DevComponents.DotNetBar.RibbonForm
    {
        public FormHangHoa()
        {
            InitializeComponent();
        }
        //private AppAccContext db = new AppAccContext();
        private PhanMenKeToanEntities1 db = new PhanMenKeToanEntities1();
        private int idtam = 0;
        private void FormHangHoa_Load(object sender, EventArgs e)
        {
            GetListHangHoa();
            GetListLoaiHangHoa();
            LoadDataLoaiHang();
            LoadDataDonViTinh();
            Reset();
        }

        private void bttThemMoi_Click(object sender, EventArgs e)
        {
            var f = new FormHangHoaThem();
            f.ShowDialog();
        }

        private void bttChinhSua_Click(object sender, EventArgs e)
        {
            var f = new FormHangHoaCapNhap();
            f.id = idtam;
            f.ShowDialog();
        }

        private void bttXoa_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void bttLamMoi_Click(object sender, EventArgs e)
        {
            GetListHangHoa();
            GetListLoaiHangHoa();
            LoadDataLoaiHang();
            LoadDataDonViTinh();
            Reset();
            dgvHangHoa.Update();
            dgvHangHoa.Refresh();
            
        }

        private void bttThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void txtTimKiem_Click(object sender, EventArgs e)
        {
            TimKiem();
        }
        
        private void dgvHangHoa_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowindex = e.RowIndex;
            var row = dgvHangHoa.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
        }
        
        private void dgvHangHoa_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int rowindex = e.RowIndex;
            var row = dgvHangHoa.Rows[rowindex];
            idtam = Convert.ToInt32(row.Cells[0].Value);
            KiemTra();
            var f = new FormHangHoaCapNhap();
            f.id = idtam;
            f.ShowDialog();
        }
        private void FormHangHoa_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.dgvHangHoa.DataSource = null;
        }
        void GetListHangHoa()
        {
            dgvHangHoa.DataSource = db.HangHoas.ToList();
        }

        void GetListLoaiHangHoa()
        {
            cbbLoaiHang.DataSource = db.LoaiHangs.ToList();
            cbbLoaiHang.DisplayMember = "TenLoaiHang";
            cbbLoaiHang.ValueMember = "Id";
            cbbLoaiHang.DropDownColumns = "Id,TenLoaiHang";
            cbbLoaiHang.DropDownColumnsHeaders = "Mã loại hàng\r\nTên loại hàng\r\n";
        }
        void Reset()
        {
            idtam = 0;
            bttChinhSua.Enabled = false;
            bttXoa.Enabled = false;
        }
        void KiemTra()
        {
            if (idtam != 0)
            {
                bttChinhSua.Enabled = true;
                bttXoa.Enabled = true;
            }
            else
            {
                bttChinhSua.Enabled = false;
                bttXoa.Enabled = false;
            }
        }
        void Delete()
        {
            if (idtam != 0)
            {
                var t = db.HangHoas.Find(idtam);
                if (
                    MessageBox.Show("Bạn muốn xóa hàng hóac: " + t.TenHangHoa + "!", "Xác nhận", MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        db.HangHoas.Remove(t);
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }

                }
            }
            else
            {
                MessageBox.Show("Bạn chưa chọn dữ liệu để xóa!");
            }
        }

        void TimKiem()
        {
            var idloaihang = Convert.ToInt32(cbbLoaiHang.SelectedValue);
            if (txtKeyWord.Text != "" && idloaihang != 0)
            {
                dgvHangHoa.DataSource =
                    db.HangHoas.Where(
                        t =>
                            t.LoaiHangId == idloaihang &&
                            t.TenHangHoa.Contains(txtKeyWord.Text)).ToList();
            }
            else if (txtKeyWord.Text == "" && idloaihang != 0)
            {
                dgvHangHoa.DataSource =
                    db.HangHoas.Where(t =>t.LoaiHangId == idloaihang).ToList();
            }
            else if (txtKeyWord.Text != "" && idloaihang == 0)
            {
                dgvHangHoa.DataSource =
                    db.HangHoas.Where(
                        t => t.TenHangHoa.Contains(txtKeyWord.Text)).ToList();
            }
        }

       void LoadDataLoaiHang()
        {
            var l = dgvHangHoa.Columns["LoaiHangId"] as DataGridViewComboBoxColumn;
            l.DataSource = db.LoaiHangs.ToList();
            l.DisplayMember = "TenLoaiHang";
            l.ValueMember = "Id";
        }

        void LoadDataDonViTinh()
        {
            var l = dgvHangHoa.Columns["DonViTinhId"] as DataGridViewComboBoxColumn;
            l.DataSource = db.DonViTinhs.ToList();
            l.DisplayMember = "TenDonViTinh";
            l.ValueMember = "Id";
        }
    }
}
